
package com.j2eefast.produce.pms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.j2eefast.common.core.base.entity.Ztree;
import com.j2eefast.common.core.exception.RxcException;
import com.j2eefast.common.core.page.Query;
import com.j2eefast.common.core.utils.MapUtil;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.common.core.utils.SpringUtil;
import com.j2eefast.common.core.utils.ToolUtil;
import com.j2eefast.framework.annotation.DataFilter;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.UserUtils;
import com.j2eefast.produce.pms.entity.PmsTemplateTypeEntity;
import com.j2eefast.produce.pms.entity.PmsTestProjectClassifyEntity;
import com.j2eefast.produce.pms.mapper.PmsTemplateTypeMapper;
import com.j2eefast.produce.pms.mapper.PmsTestProjectClassifyMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>模板分类</p>
 *
 * @author: summer
 * @date: 2021-05-29 16:47
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */
@Service
@Transactional(readOnly = true)
public class PmsTemplateTypeService extends ServiceImpl<PmsTemplateTypeMapper, PmsTemplateTypeEntity> {

	@Resource
	protected PmsTemplateTypeMapper pmsTemplateTypeMapper;


	public List<PmsTemplateTypeEntity> findList(Map<String, Object> params) {
		return SpringUtil.getAopProxy(this).getDeptList(params);
	}
	@DataFilter(compAlias="c",deptAlias = "c")
	public List<PmsTemplateTypeEntity> getDeptList(Map<String, Object> params){
		String className = (String)params.get("className");
		String parentId =(String)params.get("parentId");

		return this.baseMapper.getDeptList(
//                StrUtil.nullToDefault(className,""),
//                StrUtil.nullToDefault(parentId,""),
				(String) params.get(Constant.SQL_FILTER));
	}
	public PmsTemplateTypeEntity findCompById(Long id) {
		return pmsTemplateTypeMapper.findCompById(id);
	}
	/**
	 * 查询公司树
	 * @return
	 */
	public List<Ztree> findCompTree(Long parentId) {
		Map<String,Object> mapp = new HashMap<>();
		mapp.put("parentId",parentId);
		List<PmsTemplateTypeEntity> list = SpringUtil.getAopProxy(this).getDeptList(mapp);
		List<Ztree> ztrees = initZtree(list);
		return ztrees;
	}
	public List<Ztree> initZtree(List<PmsTemplateTypeEntity> list) {
		return  initZtree(list,null);
	}
	public List<Ztree> initZtree(List<PmsTemplateTypeEntity> compList, List<String> roleCompList){

		List<Ztree> ztrees = new ArrayList<Ztree>();
		boolean isCheck = !ToolUtil.isEmpty(roleCompList);
		for (PmsTemplateTypeEntity comp : compList) {
			Ztree ztree = new Ztree();
			ztree.setId(comp.getId());
			ztree.setpId(comp.getParentId());
			ztree.setName(comp.getName());
			ztree.setTitle(comp.getName());
			ztrees.add(ztree);
		}
		return ztrees;
	}

	public void checkDataScope(Long... ids) {
//        if (!UserUtils.hasAnyRoleKeys(Constant.SU_ADMIN)) {
		//check current user has data scope for the deleted use
		Map<String, Object> params = Maps.newConcurrentMap();
		params.put("ids", ids);
		List<?> listData =SpringUtil.getAopProxy(this).getDeptList(params);
		if (null == listData || listData.size() == 0 || listData.size() != ids.length) {
			throw new RxcException(ToolUtil.message("illegal request"),"50001");
		}
//        }
	}

	public boolean checkCompNameUnique(PmsTemplateTypeEntity comp) {
		Long compId = ToolUtil.isEmpty(comp.getId()) ? -1L : comp.getId();
		PmsTemplateTypeEntity info = this.getOne(new QueryWrapper<PmsTemplateTypeEntity>().
				eq("id",comp.getId()).eq("parent_id",comp.getParentId()));
		if (ToolUtil.isNotEmpty(info) && info.getId().longValue() != compId.longValue())
		{
			return  false;
		}
		return true;
	}

	@Transactional(rollbackFor = Exception.class)
	public boolean add(PmsTemplateTypeEntity comp){
		Date date = new Date();//创建一个时间对象，获取到当前的时间
		comp.setCreateTime(date);
		String userId= UserUtils.getUserInfo().getName();
		comp.setCreateBy(userId);
		if(this.save(comp)){
			return true;
		}
		return false;
	}
	@Transactional(rollbackFor = Exception.class)
	public boolean update(PmsTemplateTypeEntity comp){
		if(this.updateById(comp)){
			return true;
		}
		return false;
	}
	/**
	 * 通过id删除
	 * @param
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public boolean delSysCompById(Long id) {

		// 先判断是否有子公司
		List<PmsTemplateTypeEntity> list = this.listByMap(new MapUtil().put("parent_id", id));
		if (ToolUtil.isNotEmpty(list)) {
			throw new RxcException("请先删除子分类","50001");
		}
		// 在判断公司是否有分配到用户上面如果改公司已经分配到用户上,先删除用户在删
		List<PmsTemplateTypeEntity> users = pmsTemplateTypeMapper.listByMap(id);
		if (ToolUtil.isNotEmpty(users)) {
			throw new RxcException("请先删除关联分类","50001");
		}
		//删除关联地区
		//   busClassifyMapper.removeByMap(id);

		if(this.removeById(id)){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 页面展示查询翻页
	 */
	public PageUtil findPage(Map<String, Object> params) {
//		String sampleCode = (String) params.get("sampleCode");
//		String sampleType = (String) params.get("sampleType");


		Page<PmsTemplateTypeEntity> page = pmsTemplateTypeMapper.findPage(
				new Query<PmsTemplateTypeEntity>(params).getPage(),
//				StrUtil.nullToDefault(sampleCode,""),
//				StrUtil.nullToDefault(sampleType,""),
				(String) params.get(Constant.SQL_FILTER));
		return new PageUtil(page);
	}
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteByIds(Long[] ids) {

		return pmsTemplateTypeMapper.deleteByIds(ids);

	}

}
